package jdmx.framework.service.patterns.typed;

import java.sql.SQLException;
import jdmx.framework.common.Envelope;
import jdmx.framework.common.envelopes.EnvelopeTyped;
import jdmx.framework.service.Dao;
import jdmx.framework.service.ServiceContext;
import jdmx.library.da.DataTable;
import jdmx.library.da.Entity;
import jdmx.library.da.EntityList;
import jdmx.library.da.IdList;
import jdmx.library.da.SqlParam;
import jdmx.library.da.SqlStatement;
import jdmx.library.helpers.TypeHelper;

public class DaoTyped extends Dao {
    protected Orm mapper = null;

	@Override
	public void init(String handlingCode)
	{
		super.init(handlingCode);
		if (mapper == null) mapper = getMapper();
	}
	
	protected Orm getMapper() {
		String packageName = TypeHelper.getObjectPackageName(this);
		return (Orm) ServiceContext.ormFactory.createInstance(handlingCode, packageName);
	}

	@Override
	public EnvelopeTyped create(Envelope envelope) 
	{
		super.create(null);
		if (!(envelope instanceof EnvelopeTyped))
			return null;
		EnvelopeTyped requestEnvelope = (EnvelopeTyped) envelope;
		
		EnvelopeTyped resultEnvelope = new EnvelopeTyped();
		IdList idList = new IdList();
		EntityList entityList = requestEnvelope.getEntityList();
		for (Entity entity : entityList) {
			SqlStatement sqlStatement = mapper.getSqlStatement(sqlForCreate, entity);
			Integer id = null;
			try {
				id = db.execute(sqlStatement);
			} catch (SQLException e) {
			}
			idList.add(id);
		}
		resultEnvelope.setIdList(idList);
		return resultEnvelope;
	}

	@Override
	public EnvelopeTyped read(Envelope envelope) 
	{
		super.read(null);
		if (!(envelope instanceof EnvelopeTyped))
			return null;
		EnvelopeTyped requestEnvelope = (EnvelopeTyped) envelope;
		

		EnvelopeTyped resultEnvelope = new EnvelopeTyped();
		SqlStatement sqlStatement = mapper.getSqlStatement(sqlForRead, requestEnvelope.getEntityFilter());
		DataTable dataTable = null;
		try {
			dataTable = db.fetch(sqlStatement);
		} catch (SQLException e1) {
		}
		
		if (dataTable != null) {
			EntityList entityList = mapper.getEntityList(dataTable, requestEnvelope.getEntityInfo());
			resultEnvelope.setEntityList(entityList);	
		}
		return resultEnvelope;
	}

	@Override
	public EnvelopeTyped update(Envelope envelope) 
	{
		super.update(null);
		if (!(envelope instanceof EnvelopeTyped))
			return null;
		EnvelopeTyped requestEnvelope = (EnvelopeTyped) envelope;
		
		
		EntityList entityList = requestEnvelope.getEntityList();
		for (Entity entity : entityList) {
			SqlStatement sqlStatement = mapper.getSqlStatement(sqlForUpdate, entity);
			try {
				db.execute(sqlStatement);
			} catch (SQLException e) {
			}
		}
		return null;
	}
	
	@Override
	public EnvelopeTyped delete(Envelope envelope)
	{
		super.delete(null);
		if (!(envelope instanceof EnvelopeTyped))
			return null;
		EnvelopeTyped requestEnvelope = (EnvelopeTyped) envelope;
		
		
		EntityList entityList = requestEnvelope.getEntityList();
		for (Entity entity : entityList) {
			SqlStatement sqlStatement = new SqlStatement(sqlForDelete);
			sqlStatement.paramList.add(new SqlParam("id", entity.getId()));
			try {
				db.execute(sqlStatement);
			} catch (SQLException e) {
			}
		}
		return null;	
	}
}
